package cn.jhz.learn.blog.controller.core.auth;

import cn.jhz.learn.blog.common.bean.JsonData;
import cn.jhz.learn.blog.component.GLOBAL;
import cn.jhz.learn.blog.component.shiro.PermissionName;
import cn.jhz.learn.blog.service.core.auth.article.ConsoleArticleSubfieldService;
import cn.jhz.learn.blog.util.JwtUtil;
import cn.jhz.learn.blog.vo.article.query.page.ConsolePageView;
import cn.jhz.learn.blog.vo.article.submit.SubfieldParam;

import com.google.common.collect.Maps;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jose4j.jwt.MalformedClaimException;
import org.jose4j.jwt.consumer.InvalidJwtException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.Map;

@RestController
@RequestMapping("/api/auth/core/article/subfield/")
public class ConsoleArticleSubfieldController {

    private final ConsoleArticleSubfieldService subfieldService;

    @Autowired
    public ConsoleArticleSubfieldController(ConsoleArticleSubfieldService subfieldService) {
        this.subfieldService = subfieldService;
    }

    @PostMapping("create")
    @RequiresPermissions("articleSubfield:create")
    @PermissionName("createArticleSubfield")
    public JsonData createSubfield(@RequestHeader(value = GLOBAL.TOKEN_INFO.AUTH_HEADER)String jwt,
                                   @Validated @NotNull @RequestBody SubfieldParam subfield) throws MalformedClaimException,
            InvalidJwtException {

        Map<String, Object> result = Maps.newHashMap();
        result.put("subfieldId", subfieldService.create(JwtUtil.getInstance().getUid(jwt), subfield));
        return JsonData.success(result);
    }

    @DeleteMapping("{subfield_id}/del")
    @RequiresPermissions("articleSubfield:delete")
    @PermissionName("deleteArticleSubfield")
    public JsonData deleteSubfield(@RequestHeader(value = GLOBAL.TOKEN_INFO.AUTH_HEADER)String jwt,
                                  @PathVariable("subfield_id") Integer articleId,
                                  @Validated @NotNull @RequestBody Map<String, Boolean> params) throws MalformedClaimException,
            InvalidJwtException {
        subfieldService.delete(JwtUtil.getInstance().getUid(jwt), articleId, params.get("deep"));
        return JsonData.success();
    }

    @PutMapping("put")
    @RequiresPermissions("articleSubfield:put")
    @PermissionName("putArticleSubfield")
    public JsonData updateSubfield(@RequestHeader(value = GLOBAL.TOKEN_INFO.AUTH_HEADER)String jwt,
                                  @Validated @NotNull @RequestBody SubfieldParam subfield) throws MalformedClaimException, InvalidJwtException {
        subfieldService.update(JwtUtil.getInstance().getUid(jwt), subfield);
        return JsonData.success();
    }

    @PatchMapping("{subfield_id}/stick")
    @RequiresPermissions("articleSubfield:patch")
    @PermissionName("stickArticle")
    public JsonData stickSubfield(@RequestHeader(value = GLOBAL.TOKEN_INFO.AUTH_HEADER)String jwt,
                                 @PathVariable("subfield_id") Integer articleId,
                                 @Validated @NotNull @RequestBody Map<String, Boolean> params) throws MalformedClaimException, InvalidJwtException {

        subfieldService.stick(JwtUtil.getInstance().getUid(jwt), articleId, params.get("stick"));

        return JsonData.success();
    }

    @PatchMapping("{subfield_id}/rec")
    @RequiresPermissions("articleSubfield:patch")
    @PermissionName("recycleArticleSubfield")
    public JsonData recycleSubfield(@RequestHeader(value = GLOBAL.TOKEN_INFO.AUTH_HEADER)String jwt,
                                    @PathVariable("subfield_id") Integer articleId) throws MalformedClaimException, InvalidJwtException {
        subfieldService.recycle(JwtUtil.getInstance().getUid(jwt), articleId);
        return JsonData.success();
    }

    @GetMapping("count")
    @RequiresPermissions("articleSubfield:get")
    @PermissionName("getCountAndArticleSubfield")
    public JsonData getCountAndSubfield(@RequestHeader(value = GLOBAL.TOKEN_INFO.AUTH_HEADER)String jwt,
                                        @Validated @NotNull ConsolePageView pageView) throws MalformedClaimException, InvalidJwtException {
        return JsonData.success(subfieldService.getCountAndList(JwtUtil.getInstance().getUid(jwt), pageView));
    }

    @GetMapping("{subfield_id}")
    @RequiresPermissions("articleSubfield:get")
    @PermissionName("getArticleSubfieldDetail")
    public JsonData getDetail(@RequestHeader(value = GLOBAL.TOKEN_INFO.AUTH_HEADER)String jwt,
                                @PathVariable("subfield_id") Integer subfieldId) throws InvalidJwtException, MalformedClaimException {
        return JsonData.success(subfieldService.getDetail(JwtUtil.getInstance().getUid(jwt), subfieldId));
    }
}
